import router from '@/router'
import store from '@/store'

// 导航守卫
router.beforeEach(async(to, from, next) => {
  const { token } = store.getters
  if (token) {
    // 有token 去登录页 => 首页
    if (to.path === '/login') {
      next('/')
    } else {
      // 有token 不是去登录页 => 放行 next
      if (!store.state.user.userInfo.userId) {
        await store.dispatch('user/getUserInfo')
      }
      next()
    }
  } else {
    const wileList = ['/login', '/404']
    // 无token 允许不登录就查看的，放行 next
    if (wileList.includes(to.path)) {
      next()
    } else {
      // 无token 非白名单的，跳转登录页
      next('/login')
    }
  }
})
